package org.javaboy.meeting.controller;

import org.javaboy.meeting.model.Department;
import org.javaboy.meeting.model.Employee;
import org.javaboy.meeting.model.Meeting;
import org.javaboy.meeting.service.DepartmentService;
import org.javaboy.meeting.service.EmployeeService;
import org.javaboy.meeting.service.MeetingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class LoginController {

    public static final Integer CANCEL_STATUS = 1;

    @Autowired
    EmployeeService employeeService;

    @Autowired
    DepartmentService departmentService;

    @Autowired
    MeetingService meetingService;

//    @RequestMapping("/index")
//    public String login() {
//        return "index";
//    }

    @RequestMapping("/")
    public String login1(HttpSession httpSession) {
        if (httpSession.getAttribute("currentuser")!=null){
            httpSession.removeAttribute("currentuser");
        }
        return "login";
    }

    /**
     * 判断登录信息
     * @param username
     * @param password
     * @param model
     * @param httpSession
     * @param redirectAttributes
     * @return
     */
    @PostMapping("/doLogin")
    public String doLogin(String username, String password, Model model, HttpSession httpSession, HttpServletRequest request, RedirectAttributes redirectAttributes) {
        String VerificationCode = request.getParameter("yzm").toLowerCase();
        if(VerificationCode.equals(request.getSession().getAttribute("yzm"))){
            Employee employee = employeeService.doLogin(username, password);
            if (employee == null) {
                model.addAttribute("error", "用户名或密码输入错误，登录失败");
                return "forward:/";
            } else {
                if (employee.getStatus() == 0) {
                    model.addAttribute("error", "用户待审批");
                    return "forward:/";
                } else if (employee.getStatus() == 2) {
                    model.addAttribute("error", "用户已被关闭，请联系管理员");
                    return "forward:/";
                } else {
                    //登录的账号密码放入到session中
                    httpSession.setAttribute("currentuser", employee);
                    List<Meeting> mymeetings = meetingService.getMyMeetingByEmpId(employee.getEmployeeid());

                    List<Meeting> cancelMeetings = meetingService.getCancelMeetingsByStatus(CANCEL_STATUS);
//                for (Meeting meeting : cancelMeetings) {
//                    System.out.println(meeting);
//                }
//                重定向使用RedirectAttributes redirectAttributes 传数据
//                redirectAttributes.addFlashAttribute("mymeetings", mymeetings);
//                redirectAttributes.addFlashAttribute("cancelMeetings", cancelMeetings);
                    return "redirect:/notificationsAgain?employeeid=" + employee.getEmployeeid();
//                return "redirect:/notifications?employeeid="+employee.getEmployeeid();
                }
            }
        }else{
            model.addAttribute("error", "验证码错误");
            return "forward:/";
        }
    }

    /**
     * 注册用户
     * @param model
     * @return
     */
    @RequestMapping({"/register", "/admin/register"})
    public String register(Model model) {
        List<Department> deps = departmentService.getAllDeps();
        model.addAttribute("deps", deps);
        return "register";
    }

    /**
     * 注册用户
     *
     * @param employee
     * @param model
     * @return
     */
    @PostMapping("/doReg")
    public String doReg(Employee employee, Model model,HttpSession session) {
        Integer result = employeeService.doReg(employee);
        Employee currentuser = (Employee) session.getAttribute("currentuser");
        if (result == 1&&currentuser.getRole()==2) {
            return "redirect:/admin/approveaccount";
        } else if(result == 1&&currentuser.getRole()==1){
            model.addAttribute("success","注册成功");
            return "forward:/register";
        } else{
            model.addAttribute("error", "注册失败");
            model.addAttribute("employee", employee);
            return "forward:/register";
        }
    }
}
